﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda ICommandLinkButton</summary>
    public class ICommandLinkButton : System.Windows.Forms.Button
    {
        /// <summary>Property: CreateParams</summary>
        protected override System.Windows.Forms.CreateParams CreateParams
        {
            get
            {
                System.Windows.Forms.CreateParams P = base.CreateParams; // Ziskam predchodcu
                P.Style |= IWin32.BS_COMMANDLINK; // Nastavim styl
                return (P); // Vratim upraveny objekt
            }
        }
        /// <summary>Property: CalculatedAutoHeight</summary>
        protected int CalculatedAutoHeight
        {
            get
            {
                IWin32.SIZE S1 = new IWin32.SIZE(Width, 0);
                IWin32.User32.SendMessage(Handle, IWin32.BCM_GETIDEALSIZE, IntPtr.Zero, out S1); // Volam ziskanie rozmerov
                return S1.cy < IButton.MIN_BUTTON_HEIGHT ? IButton.MIN_BUTTON_HEIGHT : S1.cy;
            }
        }

        /// <summary>Konstruktor</summary>
        public ICommandLinkButton(string pText, string pNote, int pWidth = -1)
        {
            this.FlatStyle = System.Windows.Forms.FlatStyle.System;
            Text = pText;
            IWin32.User32.SendMessage(Handle, IWin32.BCM_SETNOTE, IntPtr.Zero, pNote);

            if (pWidth == -1)
            {
                MinimumSize = new System.Drawing.Size(IButton.MIN_BUTTON_WIDTH, IButton.MIN_BUTTON_HEIGHT);
                AutoSize = true;
            }
            else
            {
                Width = pWidth;
                int H = CalculatedAutoHeight;
                MinimumSize = new System.Drawing.Size(pWidth, H);
                MaximumSize = new System.Drawing.Size(pWidth, H);
                AutoSize = true;
            }
        }
    }

    /// <summary>Trieda ICommandLinkButton</summary>
    public class IDefCommandLinkButton : ICommandLinkButton
    {
        /// <summary>Property: CreateParams</summary>
        protected override System.Windows.Forms.CreateParams CreateParams
        {
            get
            {
                System.Windows.Forms.CreateParams P = base.CreateParams; // Ziskam predchodcu
                P.Style |= IWin32.BS_DEFCOMMANDLINK; // Nastavim styl
                return (P); // Vratim upraveny objekt
            }
        }

        /// <summary>Konstruktor</summary>
        public IDefCommandLinkButton(string pText, string pNote, int pWidth = -1)
            : base(pText, pNote, pWidth)
        {
        }
    }

    /// <summary>Trieda ICommandLinkButtonsPanel</summary>
	public class ICommandLinkButtonsPanel : System.Windows.Forms.UserControl
	{
        protected System.Windows.Forms.Control[] FButtons; // Zoznam tlacidiel

        public int ClientHeight; // Skutocna hlbka stlpca tlacidiel
        public int ClickedButtonIndex; // Index stlaceneho tlacidla
        public event EventHandler ButtonClick; // Event: ButtonClick

        /// <summary>Property: ControlShrinkValue</summary>
        public int ControlShrinkValue
        {
            get
            {
                return ClientSize.Height - ClientHeight;
            }
        }

        /// <summary>Event: Click pre tlacidla</summary>
        /// <param name="pSender">zdroj</param>
        /// <param name="e">parameter</param>
        protected void Event_BtnClick(object pSender, EventArgs e)
        {
            ClickedButtonIndex = ICore.ToInt(((ICommandLinkButton)pSender).Tag); // Ulozim index stlaceneho tlacidla
            if (ButtonClick != null) ButtonClick(this, new EventArgs());
        }

        /// <summary>Konstruktor</summary>
        public ICommandLinkButtonsPanel()
        {
            FButtons = null; // Inicializacia
            ClientHeight = 0;
            ClickedButtonIndex = 0;
            AutoScroll = true;
            ButtonClick = null;
        }

        /// <summary>Vytvorenie pola tlacidiel</summary>
        /// <param name="pNames">nazvy tlacidiel</param>
        /// <param name="pNotes">popisy tlacidiel</param>
        /// <param name="pDefaultChoice">prednastavena volba</param>
        /// <param name="pSeparator">oddelovac nazvov a popisov</param>
        public void CreateButtons(string pNames, string pNotes, int pDefaultChoice, char pSeparator = '\b')
        {
            try
            {
                int W = ClientSize.Width - 18; // Sirka tlacidla

                string[] Names = pNames.Split(new char[] { pSeparator }, StringSplitOptions.None); // Rozdelim vstupy
                string[] Notes = pNotes.Split(new char[] { pSeparator }, StringSplitOptions.None);

                int C = Names.Length > Notes.Length ? Notes.Length : Names.Length; // Pocet je mensi z nich
                FButtons = new System.Windows.Forms.Control[C]; // Alokujem pole pre ulozenie tlacidiel

                if (pDefaultChoice < 0) pDefaultChoice = 0;
                for (int i = 0; i < C; i++) // Vytvorim pole tlacidiel
                {
                    FButtons[i] = i == pDefaultChoice ? new IDefCommandLinkButton(Names[i], Notes[i], W) : new ICommandLinkButton(Names[i], Notes[i], W);
                    FButtons[i].Tag = i; FButtons[i].Click += Event_BtnClick;
                }

                Controls.Add(FButtons[pDefaultChoice]); // Zaradim prednastavene tlacidlo
                Controls.AddRange(FButtons); // Zaradim ostatne tlacidla

                ClientHeight = 0; // Umiestnim tlacidla
                for (int i = 0; i < FButtons.Length; i++)
                {
                    FButtons[i].Top = ClientHeight; FButtons[i].Left = 0;
                    ClientHeight = FButtons[i].Bottom + 2;
                }
            }
            catch
            {
            }
        }

        public void SetButtonsKeyDownEvent(System.Windows.Forms.KeyEventHandler pHandler)
        {
            foreach (System.Windows.Forms.Control C in FButtons) C.KeyDown += pHandler;
        }
    }
}
